<template>
	<view class="content fafafa padding">
		<view class="ffffff padding margin_bottom3 border_2" v-for="item in list.data" :key="item.id" @click="checkAddress(item.id)">
			<view class="color_ling text_w flexleft">
				<view class="margin_right2">{{item.name}}</view>
				<view>{{item.mobile}}</view>
			</view>
			<view class="margin_top flexleft padding_bottom" style="border-bottom: 1rpx solid rgba(153, 153, 153, .1);">
				<view>
					<image src="/static/image/mine/address.png" mode="" class="addressImg margin_right2"></image>
				</view>
				<view class="text_five color_san">{{item.region+item.address}}</view>
			</view>
			<view class="margin_top flexbetween">
				<view class="flexleft" @click.stop="setdefault(item)">
					<view>
						<image src="/static/image/home/xuanzhong.png" v-if="item.is_default==1" mode=""
							class="defaultImg margin_right2"></image>
						<image src="/static/image/home/wiexuanzhong.png" mode="" v-if="item.is_default==0"
							class="defaultImg margin_right2"></image>
					</view>
					<view class="color_san text_b" v-if="item.is_default==1">默认地址</view>
					<view class="color_san text_b" v-if="item.is_default==0">设为默认</view>
				</view>
				<view class="flexleft">
					<view class="flexleft margin_right6" @click.stop="goAddAddress(item.id)">
						<image src="/static/image/mine/edit.png" class="editImage margin_right2" mode="widthFix"></image>
						<view class="text_b color_san">编辑</view>
					</view>
					<view class="flexleft" @click.stop="del(item.id)">
						<image src="/static/image/mine/delete.png" class="deleteImage margin_right2" mode="widthFix"></image>
						<view class="text_b color_san">删除</view>
					</view>
				</view>
			</view>
		</view>
		<view class="flexcenter placeholderBoxTop" v-if="!list.data.length">
			<image class="placeholderBox" src="/static/placeholderimg.png" mode="widthFix"></image>
		</view>
		<!-- 底部 -->
		<view class="footer ffffff padding">
			<view class="footerButton" @click="goAddAddress('')">
				+新增收货地址
			</view>
		</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				page: 1,
				limit: 12,
				list: {
					data: []
				},
				type: ""
			}
		},
		onLoad(options) {
			this.type = options.type
		},
		onShow() {
			this.init();
		},
		methods: {
			init(isPage, page) {
				let _this = this;
				httpRequest.request('/api/address/getlist', 'GET', {
					page: page || 1,
					limit: _this.limit,
				}, false, false, true).then(res => {
					if (res.code == 1) {
						let resList = res.data;
						let dataList = _this.list;
						if (isPage == true) {
							_this.list.data = dataList.data.concat(resList.data);
						} else {
							this.list = res.data
							_this.list.data = resList.data;
						}
					}

				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			onReachBottom() {
				let _this = this;
				let list = _this.list;
				let page = _this.page;
				if (list.last_page <= page) {
					return false;
				}
				page++;
				_this.page = page;
				_this.init(true, page);
			},
			del(id) {
				httpRequest.modal('提示', '请再次确认', true, (res) => {
					if (res) {
						httpRequest.request('/api/address/del', 'POST', {
							id
						}).then(res => {
							httpRequest.toast(res.msg)
							if (res.code == 1) {
								this.init()
							}
						})
					}
				})
			},
			//设为默认
			setdefault(item) {
				if (!item.is_default) {

					httpRequest.request('/api/address/setdefault', 'POST', {
						id: item.id
					}).then(res => {
						httpRequest.toast(res.msg)
						if (res.code == 1) {
							this.init()
						}
					})
				}
			},
			goAddAddress(id) {
				uni.navigateTo({
					url: "/pages_mine/addAddress?id=" + id || ""
				})
			},
			checkAddress(id){
				if(this.type==1){
					uni.$emit("selectAddressFn",id)
					uni.navigateBack(1)
				}
			
			},
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		padding-bottom: 150rpx;
	}

	.addressImg {
		width: 26rpx;
		height: 31rpx;
	}

	.defaultImg {
		width: 24rpx;
		height: 24rpx;
		display: block;
	}

	.editImage {
		width: 24rpx;
		height: 28rpx;
	}

	.deleteImage {
		width: 28rpx;
		height: 28rpx;
	}
</style>